/**
* @author:kellen
* @contact:xiekellen@hotmail.com
* @company:achivo-gz
* @created on:
* @modified on:
*/

package com.sasis.dao.ibatis;

import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.Seat;
import com.sasis.dao.SeatDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class SeatDaoiBatis extends BaseDaoiBATIS implements SeatDao {

    /**
     * @see com.sasis.dao.SeatDao#getSeats(com.sasis.model.Seat)
     */
    public List getSeats(final Seat seat) {
          return getSqlMapClientTemplate().queryForList("getSeats", seat);
    }

    /**
     * @see com.sasis.dao.SeatDao#getSeat(Integer id)
     */
    public Seat getSeat(Integer id) {
        Seat seat = (Seat) getSqlMapClientTemplate().queryForObject("getSeat", id);

        if (seat == null) {
            throw new ObjectRetrievalFailureException(Seat.class, id);
        }

        return seat;
    }

    /**
     * @see com.sasis.dao.SeatDao#saveSeat(Seat seat)
     */    
    public void saveSeat(final Seat seat) {
        Integer id = seat.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addSeat", seat);
        } else {
            getSqlMapClientTemplate().update("updateSeat", seat);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(Seat.class, id);
        }
    }

    /**
     * @see com.sasis.dao.SeatDao#removeSeat(Integer id)
     */
    public void removeSeat(Seat seat) {
        getSqlMapClientTemplate().update("deleteSeat",  seat);
    }
}
